package com.pz.cloud.framework.leagueMem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMemFee;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemFeeService;
import com.pz.cloud.framework.business.leagueMem.vo.EditByIdsReq;
import com.pz.cloud.framework.business.leagueMem.vo.EditFeeByIdsReq;
import com.pz.cloud.framework.business.student.entity.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 团费
 * @Author: jeecg-boot
 * @Date:   2020-12-05
 * @Version: V1.0
 */
@Api(tags="团费")
@RestController
@RequestMapping("/leagueMemFee")
@Slf4j
public class LeagueMemFeeController extends BaseController<LeagueMemFee, ILeagueMemFeeService> {
	@Autowired
	private ILeagueMemFeeService leagueMemFeeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param leagueMemFee
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "团费缴纳-分页列表查询")
	@ApiOperation(value="团费缴纳-分页列表查询", notes="团费缴纳-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent = "league/LeagueMembershipDuesList")
	public Result<?> queryPageList(LeagueMemFee leagueMemFee,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		IPage<LeagueMemFee> pageList = leagueMemFeeService.findPage(leagueMemFee, req.getParameterMap());
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param leagueMemFee
	 * @return
	 */
	@AutoLog(value = "团费缴纳-添加")
	@ApiOperation(value="团费缴纳-添加", notes="团费缴纳-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LeagueMemFee leagueMemFee) {
		leagueMemFeeService.save(leagueMemFee);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param leagueMemFee
	 * @return
	 */
	@AutoLog(value = "团费缴纳-编辑")
	@ApiOperation(value="团费缴纳-编辑", notes="团费缴纳-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LeagueMemFee leagueMemFee) {
		leagueMemFeeService.updateById(leagueMemFee);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "团费缴纳-通过id删除")
	@ApiOperation(value="团费缴纳-通过id删除", notes="团费缴纳-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		leagueMemFeeService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "团费缴纳-批量删除")
	@ApiOperation(value="团费缴纳-批量删除", notes="团费缴纳-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.leagueMemFeeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "团费缴纳-通过id查询")
	@ApiOperation(value="团费缴纳-通过id查询", notes="团费缴纳-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LeagueMemFee leagueMemFee = leagueMemFeeService.getById(id);
		if(leagueMemFee==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(leagueMemFee);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param leagueMemFee
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LeagueMemFee leagueMemFee) {
		List<LeagueMemFee> list = leagueMemFeeService.list(leagueMemFee, request.getParameterMap());
		return super.exportXls(request, list, LeagueMemFee.class, "团员缴纳");
    }

//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, LeagueMemFee.class);
//    }


	@AutoLog(value = "团费-批量修改")
	@ApiOperation(value="团费-批量修改", notes="团费-批量修改")
	@RequestMapping(value = "/editByIds", method = RequestMethod.POST)
	public Result<?> editByIds(HttpServletRequest request, @RequestBody EditFeeByIdsReq req) {
		LambdaUpdateWrapper<LeagueMemFee> wrapper = Wrappers.lambdaUpdate();
		wrapper.set(LeagueMemFee::getPayStatus, req.getPayStatus());
		wrapper.in(LeagueMemFee::getId, req.getIds());
		boolean update = service.update(wrapper);
		return update ? Result.OK() : Result.error("更新失败");
	}
}
